package com.smartwebee.android.blespp.hospital;

import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.google.gson.Gson;
import com.smartwebee.android.blespp.R;
import com.smartwebee.android.blespp.bean.AllQuestions;
import com.smartwebee.android.blespp.databinding.ActivityScoreDetailBinding;
import com.smartwebee.android.blespp.hospital.adapter.ScoreAdapter;
import com.smartwebee.android.blespp.hospital.fragment.SelfTestFragment;
import com.smartwebee.android.blespp.http.HttpRequest;
import com.smartwebee.android.blespp.http.RequestInterface;
import com.smartwebee.android.blespp.http.VolleyReqQueue;
import com.smartwebee.android.blespp.resp.ScoreResp;
import com.smartwebee.android.blespp.utils.UIUtils;
import com.smartwebee.android.blespp.utils.Utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class ScoreDetailActivity extends BaseActivity implements RequestInterface {
    ActivityScoreDetailBinding binding;
    private String title;
    private ScoreAdapter adapter;
    private String uploadType;
    private String score;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TAG = "ScoreDetailActivity";
        requestQueue = VolleyReqQueue.getInstance(this).getRequestQueue();
        binding = DataBindingUtil.setContentView(this, R.layout.activity_score_detail);
        fetchBundle();
        initView();
        httpFindScoreList();
    }

    private void httpFindScoreList() {
        showLoading();
        HttpRequest httpRequest = new HttpRequest(Request.Method.POST, Utils.findUserScoreList,
                this) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("sn", Utils.registerResp.getData().getSn());
                return map;
            }
        };
        requestQueue.add(httpRequest);
    }

    private void initView() {
        binding.tvName.setText("姓名: " + Utils.registerResp.getData().getName());
        binding.tvGender.setText("性别: " + Utils.registerResp.getData().getSex());
        binding.tvSn.setText("就诊号: " + Utils.registerResp.getData().getSn());
        binding.btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        binding.tvHead.setText(title);
        adapter = new ScoreAdapter(this, new ArrayList<String>());
        binding.listview.setAdapter(adapter);
    }

    private void fetchBundle() {
        title = getIntent().getStringExtra(SelfTestFragment.INTENT_TITLE);
        if (title.equals(SelfTestFragment.TYPE1)) {
            uploadType = "1";
        } else if (title.equals(SelfTestFragment.TYPE2)) {
            uploadType = "2";
        } else if (title.equals(SelfTestFragment.TYPE3)) {
            uploadType = "3";
        } else if (title.equals(SelfTestFragment.TYPE4)) {
            uploadType = "4";
        }
    }

    @Override
    public void onSuccess(String id, String resp) {
        dismissLoading();
        ScoreResp scoreResp = new Gson().fromJson(resp, ScoreResp.class);
        if (Utils.parseData(scoreResp)) {
            for (int i = 0; i < scoreResp.getData().size(); i++) {
                ScoreResp.DataBean bean = scoreResp.getData().get(i);
                if (uploadType.equals(bean.getSid())) {
                    binding.tvDate.setText("日期: " + bean.getCreateTime());
                    score = bean.getScore();
                    binding.tvScoreSum.setText("总分: " + score);
                    String detail = bean.getDetail();
                    if (!TextUtils.isEmpty(detail)) {
                        String[] questions = detail.split(",");
                        adapter.setItems(Arrays.asList(questions));
                    }
                }
            }
        }
    }

    @Override
    public void onError(String error) {
        dismissLoading();
        UIUtils.showTips(this, error);
    }
}
